package com.example.content_android.me;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.example.content_android.LoginActivity;
import com.example.content_android.MainActivity;
import com.example.content_android.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

public class SettingActivity extends AppCompatActivity {
    public EditText oldPassword = null;
    public EditText newPassword = null;
    public EditText confirmPassword = null;
    private SharedPreferences sp;
    private boolean isVisitor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏界面默认标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        //设置顶部状态栏黑底白字（最上面这一行显示电池、时钟、wifi等图标是状态栏）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.BLACK);
        }

        setContentView(R.layout.activity_setting);
        getData();

    }

    public void modify(View v) {
        String oPassword = oldPassword.getText().toString();
        String nPassword = newPassword.getText().toString();
        String cPassword = confirmPassword.getText().toString();


        if (TextUtils.isEmpty(oPassword))//判断内容是否为空
        {
            Toast.makeText(this, "旧密码为空！", Toast.LENGTH_SHORT).show();

            return;
        }
        if (TextUtils.isEmpty(nPassword)) {
            Toast.makeText(this, "新密码为空！", Toast.LENGTH_SHORT).show();

            return;
        }
        if (TextUtils.isEmpty(cPassword)) {
            Toast.makeText(this, "确认密码为空！", Toast.LENGTH_SHORT).show();

            return;
        }
        if (!nPassword.equals(cPassword)) {
            Toast.makeText(this, "两次输入密码不一致！", Toast.LENGTH_SHORT).show();

            return;
        }
        String url = "https://www.turkeylike.cn:8900/fzulab/sys/mPassword";
        MediaType JSONType = MediaType.parse("application/json; charset=utf-8");
        JSONObject json = new JSONObject();

        SharedPreferences pref = getSharedPreferences("userInfo", MODE_PRIVATE);

        String userName = pref.getString("userName", "");

        try {
            json.put("userName", userName);
            json.put("password", nPassword);
            json.put("oldpassword", oPassword);

        } catch (JSONException e) {
            e.printStackTrace();
        }


        OkHttpUtils
                .postString()
                .url(url)
                .content(json.toString())
                .mediaType(MediaType.parse("application/json; charset=utf-8")).build().execute(
                new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("onResponse", "response");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        JSONObject responseStr = JSON.parseObject(response);
                        if( responseStr.getInteger("code")==500){
                            String msg=responseStr.getString("msg");

                            Toast.makeText(getBaseContext(),msg,Toast.LENGTH_SHORT).show();
                            return;

                        }
                        if( responseStr.getInteger("code")==0){
                            String msg=responseStr.getString("msg");

                            Toast.makeText(getBaseContext(),"密码已修改，请重新登录！",Toast.LENGTH_SHORT).show();



                            Intent intent = new Intent(SettingActivity.this, MainActivity.class);

//                            FragmentManager fragmentManager = getSupportFragmentManager();
//
//                            Button button= getSupportFragmentManager().findFragmentById(R.id.fragment_me).getView().findViewById(R.id.logout);
                            SharedPreferences sp = getSharedPreferences("userInfo", MODE_PRIVATE);

                            isVisitor = sp.getBoolean("isVisitor", true);
                            if (sp.contains("isVisitor")) {
                                isVisitor = sp.getBoolean("isVisitor", true);
                                if (!isVisitor) {
                                    SharedPreferences.Editor editor = sp.edit();
                                    editor.putBoolean("isVisitor", true);
                                    editor.commit();
                                }
                            }
//                            loginButton.setVisibility(View.VISIBLE);
//                            logoutButton.setVisibility(View.GONE);

                            intent.putExtra("id", 1);

                            startActivity(intent);
                        }
                        Log.e("onResponse", "response");
                    }

                });


    }


    public void getData() {
        oldPassword = findViewById(R.id.oldPassword);
        newPassword = findViewById(R.id.newPassword);
        confirmPassword = findViewById(R.id.confirmPassword);
    }
}
